$().ready(function() {
	validateRule();
	$("#types").val(sessionStorage.getItem('types'));
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/jzweb/examinationBatch/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}

function validateRule() {
	// $.validator.addMethod("timeDuration", function(value, element) {
	// 	var startTime = new Date($("#startTime").val()).getTime();
	// 	var endTime = new Date($("#endTime").val()).getTime();
	// 	var timeDiff = endTime - startTime;
	// 	var timeDiffInMinutes = timeDiff / (1000 * 60);
	// 	return timeDiffInMinutes <= 70;
	// }, "请将竞赛时间设置为70分钟");
	//
	// $.validator.addMethod("timeDurations", function(value, element) {
	// 	var startTime = new Date($("#startTimes").val()).getTime();
	// 	var endTime = new Date($("#endTimes").val()).getTime();
	// 	var timeDiff = endTime - startTime;
	// 	var timeDiffInMinutes = timeDiff / (1000 * 60);
	// 	return timeDiffInMinutes <= 60;
	// }, "请将竞赛时间设置为60分钟");
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			},
			cbatchId : {
				required : true
			},
			passMark : {
				required : true
			},
			totalScore : {
				required : true
			},
			// type:{
			// 	required : true
			// },
			startTime:{
				required : true,
				// timeDuration: true
			},
			endTime:{
				required : true,
				// timeDuration: true
			},
			type : {
				required : true
			},
			batchType:{
				required : true
			},
			startTimes:{
				required : true,
				// timeDurations:true
			},
			endTimes:{
				required : true,
				// timeDurations:true
			}
		},
		messages : {
			cbatchId : {
				required : icon + "请选择竞赛批次"
			},
			name : {
				required : icon + "请输入竞赛名称"
			},
			passMark : {
				required : icon + "请输入合格分"
			},
			totalScore : {
				required : icon + "请输入总分"
			},
			// type : {
			// 	required : icon + "请选择考试类型"
			// },
			startTime:{
				required : icon + "请添加竞赛开始时间"
			},
			endTime:{
				required : icon + "请添加竞赛结束时间"
			},
			type : {
				required : icon + "请选择竞赛类型"
			},
			batchType:{
				required : icon + "请选择批次类型"
			},
			startTimes:{
				required : icon + "请添加PPT上传开始时间"
			},
			endTimes:{
				required : icon + "请添加PPT上传结束时间"
			}
		}
	})
}

layui.use('laydate', function() {
	var laydate = layui.laydate;
	laydate.render({
		elem: '#startTime'
		,type: 'datetime'
	});
	laydate.render({
		elem: '#endTime'
		,type: 'datetime'
	});
	laydate.render({
		elem: '#startTimes'
		,type: 'datetime'
	});
	laydate.render({
		elem: '#endTimes'
		,type: 'datetime'
	});
})